package com.zh.harmony.butterknife.util;

import com.zh.harmony.butterknife.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class ToastUtil {
    private ToastUtil() {
    }

    public static void toast(Context context, String msg) {
        context = context.getApplicationContext();
        Component toastLayout = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_toast, null, false);
        Text msgComponent = (Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast);
        msgComponent.setText(msg);
        ToastDialog toast = new ToastDialog(context);
        toast.setText(msg)
                .setContentCustomComponent(toastLayout)
                .setAlignment(LayoutAlignment.CENTER)
                .setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT)
                //去掉原始的背景，使用xml中定义的背景颜色
                .setTransparent(true)
                .show();
    }
}